﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TimKen.DataEntity;
using TimKen.BussLogic;
using TimKen.App;
using System.Net.Mail;
using System.Configuration;
using System.IO;

namespace TimKen.Web.Admin.WebUser
{
    public partial class WebUser_Info : TimKen.App.AdminPage
    {
        private WebUserManager wum = new WebUserManager();
        public override string ActionCode
        {
            get
            {
                return "84";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["UserName"] != null)
                {
                    UserName = Request.QueryString["UserName"];
                }

                BindResources();
                BindData();

            }


            ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.btnDownLoadBusinessLicenseCopy);
            ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.btnDownLoadTaxRegistrationCopy);

        }

        private void BindData()
        {
            TimKen.DataEntity.WebUser webuser = wum.GetModel(UserName);
            if (webuser != null)
            {
                labUserName.Text = UserName;
                txtCompany.Text = webuser.Company;
                txtEmail.Text = webuser.Email;
                txtPhone.Text = webuser.Phone;
                txtRealName.Text = webuser.RealName;

                if (webuser.IsApply)
                {
                    this.panelApplyInfo.Visible = true;

                    lblIsApply.Text = GetBoolPic(webuser.IsApply);
                    lblApplyDate.Text = webuser.ApplyDate.ToString();
                    lblBankAccount.Text = webuser.BankAccount;
                    imgBusinessLicenseCopy.ImageUrl = UploadFilePathForUser + webuser.BusinessLicenseCopy;
                    imgTaxRegistrationCopy.ImageUrl = UploadFilePathForUser + webuser.TaxRegistrationCopy;

                    lblShipAddress.Text = webuser.ShippingAddress;
                    lblShippingLinkMan.Text = webuser.ShippingLinkMan;

                    lblDeliveryAddress.Text = webuser.DeliveryAddress;
                    lblDeliveryLinkMan.Text = webuser.DeliveryLinkMan;
                }

                if (webuser.IsValid)
                {
                    this.btnSendEmailCheck.Visible = false;



                    this.ddlIsValid.Items[0].Selected = true;

                }
                else
                {
                    this.btnSendEmailCheck.Visible = true;

                    this.ddlIsValid.Items[1].Selected = true;
                }
            }
            else
                Alert(GetLangString("FindNoUser"), "WebUser_List.aspx");
        }

        protected void BindResources()
        {
            btnSave.Text = GetLangString("Save");
            btnSendEmailCheck.Text = GetLangString("SendConfirmEmail");
            this.btnDownLoadBusinessLicenseCopy.Text = GetLangString("Download");
            this.btnDownLoadTaxRegistrationCopy.Text = GetLangString("Download");


            ddlIsValid.Items.Clear();
            ddlIsValid.Items.Add(new ListItem(GetLangString("Yes"), "1"));
            ddlIsValid.Items.Add(new ListItem(GetLangString("No"), "0"));
        }

        public string UserName
        {
            get
            {
                if (ViewState["UserName"] != null)
                    return ViewState["UserName"].ToString();
                else
                    return "";
            }
            set
            {
                ViewState["UserName"] = value;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            //修改,by miaoz
            //TimKen.DataEntity.WebUser webuser = new TimKen.DataEntity.WebUser();

            TimKen.DataEntity.WebUser webuser = wum.GetModel(UserName);
            webuser.UserName = labUserName.Text;
            webuser.Company = txtCompany.Text;
            webuser.Email = txtEmail.Text;
            webuser.Phone = txtPhone.Text;
            webuser.RealName = txtRealName.Text;

            webuser.IsValid = Convert.ToBoolean(int.Parse(this.ddlIsValid.SelectedValue));
            //webuser.NickName= txtNickName.Text;
            wum.Update(webuser);

            Alert(GetLangString("SaveSuccessful"));
        }

        protected void btnSendEmailCheck_Click(object sender, EventArgs e)
        {
            string mailSendAddr = "https://www.firstbridgept.com";//发件人
            string TimKenUrl = mailSendAddr;
            //string TimKenUrl = "http://" + Request.ServerVariables["HTTP_HOST"];
            string code = Tools.EncryptUserName(UserName);
            string path = TimKenUrl + "/User/CheckUser.aspx";
            string checkcode = "<a href=\"" + path + "?code=" + code + "\">" + GetLangString("ClickHere") + "</a>";
            MailMessage mm = new MailMessage();
            MailAddress to = new MailAddress(txtEmail.Text);
            mm.To.Add(to);
            mm.Subject = GetLangString("AuthenticationUserTitle");
            mm.Body = string.Format(GetLangString("AuthenticationUserContent"), checkcode, path, code);
            mm.IsBodyHtml = true;
            Tools.SendEmail(mm);
        }

        protected void DownLoadApplyFile(string filename)
        {
            try
            {
                string path = UploadFilePathForUser + filename;


                if (System.IO.File.Exists(Server.MapPath(path)))
                {

                    string filePath = Server.MapPath(path);//路径 

                    //以字符流的形式下载文件 
                    FileStream fs = new FileStream(filePath, FileMode.Open);
                    byte[] bytes = new byte[(int)fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    Response.ContentType = "application/octet-stream";
                    //通知浏览器下载文件而不是打开 
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();
                }
                else
                {
                    Alert(GetLangString("NotFoundFile"));
                }
            }
            catch
            {
                Response.Redirect("/Error.aspx");
            }
        }

        protected void btnDownBusinessLicenseCopy_Click(object sender, EventArgs e)
        {
            TimKen.DataEntity.WebUser webuser = wum.GetModel(UserName);
            DownLoadApplyFile(webuser.BusinessLicenseCopy);
        }

        protected void btnDownTaxRegistrationCopy_Click(object sender, EventArgs e)
        {
            TimKen.DataEntity.WebUser webuser = wum.GetModel(UserName);
            DownLoadApplyFile(webuser.TaxRegistrationCopy);
        }

    }
}
